package com.sky.service.admin.impl;
import java.math.BigDecimal;
import com.google.common.collect.Lists;
import java.time.LocalDateTime;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.anno.AutoFill;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.enumeration.OperationType;
import com.sky.exception.BaseException;
import com.sky.mapper.admin.SetMealMapper;
import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.mapper.common.CategoryMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.service.admin.SetMealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Author:篍
 * @Date: 2025/08/17 21:10
 * @Description:
 */
@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page page = (Page) setMealMapper.page(setmealPageQueryDTO.getName(), setmealPageQueryDTO.getCategoryId(), setmealPageQueryDTO.getStatus());
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增套餐和套餐菜品关系
     *
     * @param setmealDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SetmealDTO setmealDTO) {
        try {
            Setmeal setmeal = new Setmeal();
            BeanUtils.copyProperties(setmealDTO, setmeal);
            setMealMapper.add(setmeal);
            List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
            setmealDishMapper.insertBatch(setmealDishes);
        } catch (BeansException e) {
            throw new BaseException("添加套餐失败");
        }
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        List<Dish> dishes=setmealDishMapper.findDishesBySetmealId(id);
        dishes.forEach(dish -> {
            if(StatusConstant.DISABLE.equals(dish.getStatus())){
                String dishName = dish.getName();
                throw new BaseException("该套餐中的"+dishName+"已停售"+",该套餐不能启售");
            }
        });
        setMealMapper.update(Setmeal.builder()
                .status(status)
                .id(id).build());
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SetmealDTO setmealDTO) {
        try {
            Long setmealId = setmealDTO.getId();
            Setmeal setmeal = new Setmeal();
            BeanUtils.copyProperties(setmealDTO, setmeal);
            setMealMapper.update(setmeal);
            setmealDishMapper.deleteBySetmealIds(Collections.singletonList(setmealId));
            List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
            setmealDishes.forEach(dish -> dish.setSetmealId(setmealId));
            setmealDishMapper.insertBatch(setmealDishes);
        } catch (BeansException e) {
            throw new BaseException("修改套餐失败");
        }
    }

    /**
     * 查看套餐详情
     * @param id
     * @return
     */
    @Override
    public SetmealVO findById(Long id) {
        Setmeal setmeal = setMealMapper.findById(id);
        SetmealVO setmealVO = new SetmealVO();
        setmealVO.setId(setmeal.getId());
        setmealVO.setCategoryId(setmeal.getCategoryId());
        setmealVO.setName(setmeal.getName());
        setmealVO.setPrice(setmeal.getPrice());
        setmealVO.setStatus(setmeal.getStatus());
        setmealVO.setDescription(setmeal.getDescription());
        setmealVO.setImage(setmeal.getImage());
        setmealVO.setCategoryName(categoryMapper.findById(setmeal.getCategoryId()).getName());
        setmealVO.setUpdateTime(setmeal.getUpdateTime());
        setmealVO.setMakeTime(setmeal.getMakeTime());
        setmealVO.setSetmealDishes(setmealDishMapper.findDishBySetmealId(setmeal.getId()));
        return setmealVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(List<Long> ids) {
        ids.forEach(id -> {
            Setmeal setmeal = setMealMapper.findById(id);
            if (StatusConstant.ENABLE.equals(setmeal.getStatus())){
                throw new BaseException("当前状态为起售状态，不能删除");
            }
        });
        setMealMapper.deleteByIds(ids);
        setmealDishMapper.deleteBySetmealIds(ids);
    }
}
